<?php
/**
 * 《功能信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Func extends InfoUtil {
	public $tbl = Constant::tbl_func;

	public $orderby = array(
		'-' => '请选择',
		'func' => '功能',
		'name' => '名称',
		'link' => '链接',
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/func/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$category_s = $request['category_s'];
		$name_s = $request['name_s'];

		$where = " WHERE 1=1";

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name = '" . $name_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['category'] = $this->category[$row['category']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/func/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/func/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$name = $request['name'];
		$link = $request['link'];
		$content = addslashes($request['content']);

		$orderby = $request['orderby'];
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, link, content, orderby, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $link, $content, $orderby, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/func/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];
		$link = $request['link'];
		$content = addslashes($request['content']);

		$orderby = $request['orderby'];
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', link = '%s', content = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $link, $content, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'info/func/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/func/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		
		$fmt = "SELECT * FROM %s WHERE status = 1";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function _valid($id) {
		$row = $this->id($this->tbl, $id);
		if ($row['status'] == '1') {
			return true;
		}
		
		return false;
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$func = $this->id($this->tbl, $id);

		$controller->assign_by_ref('func', $func);

		$db = newdb($this->dsn_r);
		$data = array();
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($this->emp_func($row['id'], $func['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/func/emp');
		return true;
	}

	function emp_func($emp, $func) {
		require_once(dirname(__FILE__) . '/Customize.class.php');
		$aa = new Customize();
		
		return $aa->emp_func($emp, $func);
	}

	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$id = $request['id'];
		
		$row = $this->id($this->tbl, $id);

		$list = $request['list'];
		if (empty($list)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return true;
		}

		require_once(dirname(__FILE__) . '/Customize.class.php');
		$aa = new Customize();
		
		$aa->_update2($id, $row['name'], $row['link'], $list);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function t($controller, $request) {
		
		$a = array(
			'基本信息' => array('cc'=>'emp.php?do=Emp.find', 'js'=>''),
		    '个人资料' => array('cc'=>'emp.php?do=Info.find', 'js'=>''),
			'入职信息' => array('cc'=>'emp.php?do=Extra.find', 'js'=>''),
		
			'合同信息' => array('cc'=>'emp.php?do=Contract.find', 'js'=>''),
			
			'家庭关系' => array('cc'=>'emp.php?do=Family.find', 'js'=>''),
			'学习经历' => array('cc'=>'emp.php?do=Education.find', 'js'=>''),
			'工作经历' => array('cc'=>'emp.php?do=Work.find', 'js'=>''),
		
			'网络信息' => array('cc'=>'emp.php?do=Mac.find', 'js'=>''),	
			'分机信息' => array('cc'=>'emp.php?do=Exten.find', 'js'=>''),
			'邮箱信息' => array('cc'=>'emp.php?do=Vpopmail.find', 'js'=>''),
		
			'招聘职位' => array('cc'=>'info.php?do=Job.find', 'js'=>''),
		
			'我的客户' => array('cc'=>'my.php?do=MyCustomer.find', 'js'=>''),
			'我的报价' => array('cc'=>'customer.php?do=Quote.find', 'js'=>''),
			'我的产品' => array('cc'=>'my.php?do=MyMold.find', 'js'=>''),
		
			'我的收款' => array('cc'=>'finance.php?do=MyIn.find', 'js'=>''),
			'收款明细' => array('cc'=>'finance.php?do=MyInItem.find', 'js'=>''),
			'我的到帐' => array('cc'=>'finance.php?do=MyTransaction.find', 'js'=>''),
		
			'报价项目' => array('cc'=>'customer.php?do=Quote.find', 'js'=>''),
			'报价信息' => array('cc'=>'customer.php?do=QuoteInfo.find', 'js'=>''),
		
			'我的报销' => array('cc'=>'my.php?do=MyExpense.find', 'js'=>''),
			'我的借款' => array('cc'=>'my.php?do=MyBorrow.find', 'js'=>''),			
			'我的付款' => array('cc'=>'my.php?do=MyPayment.find', 'js'=>''),
		
			'采购信息' => array('cc'=>'material.php?do=Purchase.find', 'js'=>''),
			'采购明细' => array('cc'=>'material.php?do=RecordIn.find', 'js'=>''),
		
			'入库明细' => array('cc'=>'material.php?do=In.find', 'js'=>''),			
			'物料库存' => array('cc'=>'material.php?do=Amount.find', 'js'=>''),
			'出库明细' => array('cc'=>'material.php?do=Out.find', 'js'=>''),
		
			'盘点信息' => array('cc'=>'material.php?do=PanDian.find', 'js'=>''),			
			'物料需求' => array('cc'=>'material.php?do=Bom.find', 'js'=>''),
		
			'外币兑换' => array('cc'=>'finance.php?do=Convert.find', 'js'=>''),
			'理财信息' => array('cc'=>'finance.php?do=Invest.find', 'js'=>''),
			
			'报销管理' => array('cc'=>'finance.php?do=Expense.find', 'js'=>''),
			'借款信息' => array('cc'=>'finance.php?do=Borrow.find', 'js'=>''),
			'付款管理' => array('cc'=>'finance.php?do=Payment.find', 'js'=>''),
			
			'银行帐号' => array('cc'=>'finance.php?do=Bank.find', 'js'=>''),
		
			'收入信息' => array('cc'=>'finance.php?do=In.find', 'js'=>''),
		    '收入明细' => array('cc'=>'finance.php?do=InItem.find', 'js'=>''),
		    '银行到帐' => array('cc'=>'finance.php?do=Transaction.find', 'js'=>''),
		
			'支出信息' => array('cc'=>'finance.php?do=Out.find', 'js'=>''),
		    '支出明细' => array('cc'=>'finance.php?do=OutItem.find', 'js'=>''),		  
		  
			'货运信息' => array('cc'=>'express.php?do=Plan.find', 'js'=>''),
			'外发加工' => array('cc'=>'mold.php?do=Process.find', 'js'=>''),
					
			'资金情况' => array('cc'=>'finance.php?do=Money.find', 'js'=>''),
		);
		
		$db = newdb($this->dsn_w);		
		while (list($name, $v) = each($a)) {
			$fmt = "INSERT INTO %s(name, link, orderby, status, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '5', '1', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $name, $v['cc']);
			echo $sql . "\n";
			$db->query($sql);
			
		}
		
		logdate($controller, $request);
		return true;
	}
}
?>